package com.ziru.ziru.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ziru.ziru.dao.RoomsDao;
import com.ziru.ziru.pojo.Rooms;
import com.ziru.ziru.service.RoomService;
import com.ziru.ziru.utils.OrderHandler;
import com.ziru.ziru.utils.RedisConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.concurrent.TimeUnit;


/**
 * @author 缘友一世
 * date 2023/5/28-9:04
 */
@Controller
@Api(tags = "房间管理接口")
@Slf4j
public class RoomController {
    @Resource
    RoomsDao roomsDao;

    @Resource
    StringRedisTemplate stringRedisTemplate;
    @Resource
    private RoomService roomService;

    /**
     *  从Redis中获取缓存的房间信息，如果存在则直接返回分页处理结果，如果不存在则从数据库中查询房间信息，并将查询结果存储到Redis中供下次使用
     * @param tj 模糊查询条件
     * @param pageNum 分页页码
     * @param rule 排序规则
     * @param flag 排序标签标记
     * @param httpSession session
     * @return
     */
    @RequestMapping("/queryAll")
    @ApiOperation("查询房间信息")
    public String queryAll(@RequestParam(value = "tj",defaultValue = "") @ApiParam("模糊查询条件参数") String tj,
                           @RequestParam(value = "pageNum",defaultValue = "1") @ApiParam("分页页码参数") String pageNum,
                           @RequestParam(value = "rule",defaultValue = "room_id") @ApiParam("排序规则参数") String rule,
                           @RequestParam(value = "flag",defaultValue = "1") @ApiParam("排序标签标记参数") String flag,
                           HttpSession httpSession) {
        List<Rooms> rooms;
        int page = Integer.parseInt(pageNum);
        PageHelper.startPage(page,6);
        //查询数据库中的房间信息
        rooms = roomService.queryAll(rule, tj);
        return dealPage(tj, page, rule, flag, httpSession, rooms);
    }

    /**
     *
     * @param tj 模糊查询条件
     * @param pageNum 分页页码
     * @param rule 排序规则
     * @param flag 排序标签标记
     * @param httpSession session
     * @param rooms 查询结果集合
     * @return
     */
    private static String dealPage(String tj, int pageNum, String rule, String flag, HttpSession httpSession, List rooms) {
        httpSession.setAttribute("rooms", rooms);
        PageInfo<Rooms> info = new PageInfo<>(rooms);
        //System.out.println("数据总条数："+info.getTotal());
        httpSession.setAttribute("info",info);
        httpSession.setAttribute("currentPage", pageNum);
        OrderHandler handler = new OrderHandler();
        flag =handler.setFlag(rule, flag);

        //存入属性，以便前端使用
        httpSession.setAttribute("rule", rule);
        httpSession.setAttribute("flag", flag);
        httpSession.setAttribute("tj", tj);

        //System.out.println(info);
        return "rent.jsp";
    }
    @ApiOperation("根据ID查询房间信息")
    @RequestMapping("/queryById")
    public String queryById(@RequestParam(value = "id") @ApiParam("房间信息ID参数") int id,HttpSession session){
        //从Redis中获取名为KEY_ROOM_ID的值，该值存储了房间信息的JSON字符串
        String roomStr = stringRedisTemplate.opsForValue().get(RedisConstant.KEY_ROOM_ID + id);
        if (StringUtils.isNotBlank(roomStr)) {
            Rooms room = JSONUtil.toBean(roomStr, Rooms.class);
            session.setAttribute("room",room);
            return "rooms.jsp";
        }
        Rooms room = roomService.getById(id);
        String roomStr1 = JSONUtil.toJsonStr(room);
        // 将房间信息JSON字符串存储到Redis中，并设置过期时间为10分钟
        stringRedisTemplate.opsForValue().set(RedisConstant.KEY_ROOM_ID, roomStr1, 3, TimeUnit.MINUTES);
        session.setAttribute("room",room);
        return "rooms.jsp";
    }
}
